package com.example.first.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.first.R;
import com.example.first.activities.MainActivity;
import com.example.first.modle.ApiResponse;
import com.example.first.modle.OrderDetail;
import com.example.first.modle.OrderVO;
import com.example.first.services.ApiService;
import com.example.first.utils.RetrofitClient;

import java.util.List;

import retrofit2.Call;

public class HistoryOrderAdapter extends BaseAdapter {

    private List<OrderVO> orders;
    private Context context;
    private ViewHolder viewHolder;

    public HistoryOrderAdapter(Context context,List<OrderVO> orders) {
        this.context = context;
        this.orders = orders;
    }

    @Override
    public int getCount() {
        return this.orders.size();
    }

    @Override
    public Object getItem(int i) {
        return orders.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.order_item, viewGroup, false);
            viewHolder = new HistoryOrderAdapter.ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        OrderVO order = orders.get(i);
        viewHolder.orderTime.setText(order.getOrderTime());
        viewHolder.orderStatus.setText(order.getStatusValue());

        List<OrderDetail> list = order.getOrderDetailList();
        int size= Math.min(list.size(), 3);
        switch (size){
            case 3:
                Glide.with(context)
                        .load(list.get(2).getImage())
                        .into(viewHolder.dish_3);
                viewHolder.dish_3_name.setText(list.get(2).getName());
                viewHolder.dish_3.setVisibility(View.VISIBLE);
                viewHolder.dish_2.setVisibility(View.VISIBLE);

            case 2:
                Glide.with(context)
                        .load(list.get(1).getImage())
                        .into(viewHolder.dish_2);
                viewHolder.dish_2_name.setText(list.get(1).getName());
                viewHolder.dish_2.setVisibility(View.VISIBLE);
                viewHolder.dish_2_name.setVisibility(View.VISIBLE);

            case 1:
                Glide.with(context)
                        .load(list.get(0).getImage())
                        .into(viewHolder.dish_1);
                viewHolder.dish_1_name.setText(list.get(0).getName());

            default:
                break;

        }
        if(order.getStatus()<5&&order.getStatus()>1){
            viewHolder.reminder.setVisibility(View.VISIBLE);
            viewHolder.reminder.setOnClickListener(v -> {
                Toast.makeText(context, "已催单,请耐心等待", Toast.LENGTH_SHORT).show();
            });
        }

        viewHolder.repetition.setOnClickListener(v -> {
            ApiService apiService= RetrofitClient.getApiService();
            Call<ApiResponse<String>> call=apiService.repetition(order.getId());
            call.enqueue(new retrofit2.Callback<ApiResponse<String>>() {
                @Override
                public void onResponse(Call<ApiResponse<String>> call, retrofit2.Response<ApiResponse<String>> response) {
                    if(response.isSuccessful()&&response.body()!=null){
                        if(response.body().getCode()==1){
                            Toast.makeText(context, "已重新加入购物车", Toast.LENGTH_SHORT).show();
                            //关闭所有activity,启动mainActivity
                            Intent intent=new Intent(context, MainActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            context.startActivity(intent);

                        }
                    }
                }
                @Override
                public void onFailure(Call<ApiResponse<String>> call, Throwable t) {

                }
            });
        });

        return view;
    }

    public void setOrders(List<OrderVO> orders) {
        this.orders = orders;
        notifyDataSetChanged();
    }


    static class ViewHolder {
        TextView orderTime;
        TextView orderStatus;
        ImageView dish_1;
        TextView dish_1_name;
        ImageView dish_2;
        TextView dish_2_name;
        ImageView dish_3;
        TextView dish_3_name;
        Button reminder;
        Button repetition;


        ViewHolder(View view) {
            orderTime=view.findViewById(R.id.order_time);
            orderStatus=view.findViewById(R.id.order_status);
            dish_1=view.findViewById(R.id.dish_image_1);
            dish_1_name=view.findViewById(R.id.dish_name_1);
            dish_2=view.findViewById(R.id.dish_image_2);
            dish_2_name=view.findViewById(R.id.dish_name_2);
            dish_3=view.findViewById(R.id.dish_image_3);
            dish_3_name=view.findViewById(R.id.dish_name_3);
            reminder=view.findViewById(R.id.reminder);
            repetition=view.findViewById(R.id.repetition);
        }
    }



}
